import os
import re
from typing import LiteralString


def read_text_file(file_name: str) -> str:
    with open(file_name, 'rt', encoding='utf-8') as f:
        b = f.read()
        f.close()
    return b


def read_binary_file(file_name: str) -> bytes:
    with open(file_name, 'rb') as f:
        b = f.read()
        f.close()
    return b


def write_to_text_file(file_output_dir: str, file_name: str, content: str):
    if not os.path.exists(file_output_dir):
        os.makedirs(file_output_dir, exist_ok=True)
    with open(os.path.join(file_output_dir, file_name), 'wt', encoding='utf8') as f:
        f.write(content)


def write_to_binary_file(file_output_dir: str, file_name: str, content: str):
    if not os.path.exists(file_output_dir):
        os.makedirs(file_output_dir, exist_ok=True)
    with open(os.path.join(file_output_dir, file_name), 'wb') as f:
        f.write(content)


def find_files_with_regex(directory: str, regex: str) -> list[str]:
    files_with_prefix = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            m = re.match(regex, file)
            if m is not None:
                files_with_prefix.append(file)
    return files_with_prefix


if __name__ == '__main__':
    write_to_text_file(r'D:\123', r'123.txt', '123')
